package edu.cibertec.sicsolutions.vfs.service;

import java.util.Map;

import org.apache.log4j.Logger;

import edu.cibertec.sicsolutions.exception.DaoException;
import edu.cibertec.sicsolutions.exception.DomainException;
import edu.cibertec.sicsolutions.vfs.dao.votacion.CedulaDao;
import edu.cibertec.sicsolutions.vfs.domain.votacion.Cedula;
import edu.cibertec.sicsolutions.vfs.domain.votacion.DetalleCedula;
import edu.cibertec.sicsolutions.vfs.spring.SpringContext;

/**
 *   
 * @author enrique.vargas
 *
 */
public class CedulaService  {

	/**
	 * 
	 */
	 
	private static final Logger log = Logger.getLogger(CedulaService.class);
	private static final CedulaDao cedulaDao = (CedulaDao)SpringContext.get(CedulaDao.class);	
 
   	
	/**
	 * Get Object of Data Base By PK	 
	 * @param cedula
	 * @return
	 * @throws DomainException
	 */
	public static Cedula getCedulaByPk(Cedula cedula) throws DomainException{
		try{
			return cedulaDao.getCedulaByPk(cedula);
		} catch (DaoException e) {
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		} catch (Exception e){
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		}		
	}
	
	/**
	 * Save Object
	 * @param cedula
	 * @return
	 * @throws DomainException
	 */	
	public static void insertCedula (Cedula cedula) throws DomainException{
		try{
			cedulaDao.insertCedula(cedula);
		//	DetalleCedulaService.insertDetalleCedula(cedula.getDetalleCedulas()); 
		} catch (DaoException e) {
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		} catch (Exception e){
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		}		
	}
	
	/**
	 * Update Object 
	 * @param cedula
	 * @return
	 * @throws DomainException
	 */
	public static void updateCedula (Cedula cedula) throws DomainException{		
		try{
			cedulaDao.updateCedula(cedula);
		} catch (DaoException e) {
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		} catch (Exception e){
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		}
	}
	
	/**
	 * Update Object 
	 * @param cedula
	 * @return
	 * @throws DomainException
	 */
	public static void updateCedulaByMap (Map map) throws DomainException{		
		try{
			cedulaDao.updateCedulaByMap(map);
		} catch (DaoException e) {
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		} catch (Exception e){
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		}
	}
	
	/**
	 * Delete Object	 
	 * @param cedula
	 * @return
	 * @throws DomainException
	 */
	public static void deleteCedulaByPk(Cedula cedula) throws DomainException{		
		try{
			cedulaDao.deleteCedulaByPk(cedula);
		} catch (DaoException e) {
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		} catch (Exception e){
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		}
	}
	
	/**
	 * Find Object By Filters
	 * @param cedula
	 * @return
	 * @throws DomainException
	 */
	public static Cedula[] findCedulaByClass(Cedula cedula) throws DomainException{		
		try{
			Cedula[] cedulas = cedulaDao.findCedulaByClass(cedula);
		//	for(int i=0; cedulas.length >0;i++ ){
				DetalleCedula detalleCedula = new DetalleCedula();
				detalleCedula.setCodigoCedula(cedulas[0].getCodigo());
				cedulas[0].setDetalleCedulas(DetalleCedulaService.findDetalleCedulaByClass(detalleCedula));
		// 	}   		
			return cedulas;
		} catch (DaoException e) {
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		} catch (Exception e){
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		}
	}

	public static Cedula[] findAllCedulas() throws DomainException{		
		try{
			return cedulaDao.findCedulaByClass(null);
		} catch (DaoException e) {
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		} catch (Exception e){
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		}
	}
	
	public static Cedula[] findCedulasByAnio(Cedula cedula) throws DomainException{		
		try{
			return cedulaDao.findCedulaByClass(cedula);
		} catch (DaoException e) {
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		} catch (Exception e){
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		}
	}

	public static Cedula[] findAniosCedula() throws DomainException{		
		try{
			return cedulaDao.findAniosCedula();
		} catch (DaoException e) {
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		} catch (Exception e){
			log.error(e.getMessage(), e);
			throw new DomainException(e.getMessage(), e);
		}
	}
 
}